package com.xiaoxu.mp.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "用户查询条件实体")
public class PageQuery {
    //默认值防止前端没有传 会导致空指针
    @ApiModelProperty("页码")
    private Integer pageNo = 1;
    @ApiModelProperty("页数量")
    private Integer pageSize = 5;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem ... items){
        // 1.构建查询条件
        // 1.1 分页条件
        Page<T> page = Page.of(pageNo,pageSize);
        // 1.2 排序条件
        if (StrUtil.isNotBlank(getSortBy())){
            page.addOrder(new OrderItem(sortBy,isAsc));
        }else if (items != null ){
            //为空，默认按照更新时间为排序
//            page.addOrder(new OrderItem("update_time",false));
            page.addOrder(items);
        }
        return page;
    }

    public <T> Page<T> toMpPage(String defaultSortBy,Boolean defaultAsc){
        return toMpPage(new OrderItem(defaultSortBy,defaultAsc));
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTime(){
        return toMpPage(new OrderItem("create_time",false));
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTime(){
        return toMpPage(new OrderItem("update_time",false));
    }

}
